package com.mobi.Survey.controller;

import java.util.Comparator;
import java.util.List;

import android.content.Context;

import com.mobi.Survey.model.AbstractTable;
import com.mobi.Survey.model.SurveyDetailTable;

/**
 * 
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * SurveyDetailPojo.java class holds data about individual control and saving it
 */
public class SurveyDetailPojo extends Pojo
{

	private String ctrlResult;

	private byte controlType;
	private boolean isMendatory;

	private String controlText;
	private String controlParameter;
	private String actualValue;
	private String userValue;
	private String defaultValue;
	private float percentHeight;
	private float percentWidth;
	private int sequanceNo;

	private long surveyId;
	private long id;
	private long subscriptionId;

	/* Position Variables are Reserved for Future */
	private float positionX;
	private float positionY;

	// To Identify individual SurveyDetail blonging to SurveyPojo

	private SurveyPojo survey;

	public SurveyPojo getSurvey()
	{
		return survey;
	}

	public void setSurvey(SurveyPojo survey)
	{
		this.survey = survey;
	}

	public SurveyDetailPojo()
	{

	}

	public SurveyDetailPojo(byte controlType, String controlText,
			String controlParameter, long subscriptionId, String actualValue)
	{
		super();
		this.controlType = controlType;
		this.controlText = controlText;
		this.controlParameter = controlParameter;
		this.subscriptionId = subscriptionId;
		this.actualValue = actualValue;

	}

	public static final class SortBySequance implements
			Comparator<SurveyDetailPojo>
	{

		@Override
		public int compare(SurveyDetailPojo lhs, SurveyDetailPojo rhs)
		{

			return lhs.getSequanceNo() - rhs.getSequanceNo();
		}

	}

	public long getId()
	{
		return id;
	}

	public SurveyDetailPojo setId(long id)
	{
		this.id = id;
		return this;
	}

	public long getSurveyId()
	{
		return surveyId;
	}

	public byte getControlType()
	{
		return controlType;
	}

	public String getControlText()
	{
		return controlText;
	}

	public String getControlParameter()
	{

		return (controlParameter == null) ? "" : controlParameter;
	}

	public static final String getDeviceId()
	{
		return com.mobi.Survey.utils.Device.getDeviceId();
	}

	public long getSubscriptionId()
	{
		return subscriptionId;
	}

	public SurveyDetailPojo setControlType(byte controlType)
	{
		this.controlType = controlType;
		return this;
	}

	public SurveyDetailPojo setControlText(String controlText)
	{
		this.controlText = controlText;
		return this;
	}

	public SurveyDetailPojo setControlParameter(String controlParameter)
	{
		this.controlParameter = controlParameter;
		return this;
	}

	public SurveyDetailPojo setSubscriptionId(long subscriptionId)
	{
		this.subscriptionId = subscriptionId;
		return this;
	}

	/**
	 * Saves survey control data in databases
	 * 
	 * @param ctx
	 *            Context of class
	 * @param surveyDetailPojos
	 *            Collection of SurveyDetailPojo of survey
	 * @param surveyId
	 *            id of survey to which detail belongs
	 * @return
	 */
	public static long commit(Context ctx,
			List<SurveyDetailPojo> surveyDetailPojos, long surveyId)
	{
		AbstractTable at = null;

		try
		{
			at = new SurveyDetailTable(ctx);
			for (SurveyDetailPojo surveyPojo : surveyDetailPojos)
			{
				at.insertRow(surveyPojo, surveyId);

			}

		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			if (at != null)
			{
				at.closeDB();
				at = null;
			}
		}

		return 3;
	}

	/**
	 * return result value of control
	 * 
	 * @return
	 */
	public String getCtrlResult()
	{
		return ctrlResult;
	}

	public void setCtrlResult(String ctrlResult)
	{
		this.ctrlResult = ctrlResult;
	}

	public SurveyDetailPojo setSurveyId(long sId)
	{

		surveyId = sId;
		return this;
	}

	public boolean isMendatory()
	{
		return isMendatory;
	}

	public SurveyDetailPojo setMendatory(boolean isMendatory)
	{
		this.isMendatory = isMendatory;
		return this;
	}

	public String getActualValue()
	{
		if (actualValue == null)
			return "";
		return actualValue;
	}

	public String getUserValue()
	{
		if (userValue == null)
			return "";
		return userValue;
	}

	public SurveyDetailPojo setActualValue(String actualValue)
	{
		this.actualValue = actualValue;
		return this;
	}

	public SurveyDetailPojo setUsertValue(String userValue)
	{
		this.userValue = userValue;
		return this;
	}

	public int getSequanceNo()
	{
		return sequanceNo;
	}

	public SurveyDetailPojo setSequanceNo(int sequanceNo)
	{
		this.sequanceNo = sequanceNo;
		return this;
	}

	public String getDefaultValue()
	{
		if (defaultValue == null)
			return "";
		return defaultValue;
	}

	public SurveyDetailPojo setUserValue(String userValue)
	{
		this.userValue = userValue;
		return this;

	}

	/**
	 * Default value for Controls(CustomEditText,
	 * CustomTimePicker,CustomDatePicker)
	 * 
	 * @param defaultValue
	 * @return
	 */
	public SurveyDetailPojo setDefaultValue(String defaultValue)
	{
		this.defaultValue = defaultValue;
		return this;
	}

	public float getPercentHeight()
	{
		return percentHeight;
	}

	public SurveyDetailPojo setPercentHeight(float percentHeight)
	{
		this.percentHeight = percentHeight;
		return this;
	}

	public float getPercentWidth()
	{
		return percentWidth;
	}

	public SurveyDetailPojo setPercentWidth(float percentWidth)
	{
		this.percentWidth = percentWidth;
		return this;
	}

	public float getPositionX()
	{
		return positionX;
	}

	public void setPositionX(float positionX)
	{
		this.positionX = positionX;
	}

	public float getPositionY()
	{
		return positionY;
	}

	public void setPositionY(float positionY)
	{
		this.positionY = positionY;
	}

	@Override
	public String toString()
	{
		// TODO Auto-generated method stub
		return "Seq: " + sequanceNo + ",Mandatory: " + isMendatory;
	}

}
